/**
 *	Handles click and mouse movement events, passing them back 
 *	to the display logic for handling
**/

"use strict"

var MouseEvents = {
	
	_debug: false,
	
	
	//(re)make the click events
	createMouseEvents: function(parent, game, localPlayer) {
		var that = this
		
		// helper function - turns the x and y co-ordinates from an event into board positions
		var eventToBoardPos = function(e) {
			var xpos = Math.floor( e.layerX / (parent.offsetWidth / game.board.w) ),
				ypos = Math.floor( e.layerY / (parent.offsetHeight / game.board.h) )
			
			return { x: xpos, y: ypos }
		}
		
		
		// this does a single move - i.e. a click event
		var doEvent = function(e1, e2) {
			//alert(JSON.stringify(game))
			
			var pos1 = eventToBoardPos(e1),
				pos2 = ( typeof e2 === "undefined" ? { } : eventToBoardPos(e2) )	// this will provide 'undefined' values to pos2.x/y below
						
			//anly allow local moves if it is not 'localPlayer's turn
			//		(unless debug mode is on, in which case allow all)
			if ( game.winner ) {
				//note: this functionality is duplicated due to logic flow
				var winner = ( game.winner == -1 ? "draw" : game.players[game.winner - 1] )
				Controllers.GamePage.userLog( "can't move, game already won by: " + winner )
			} else if ( MouseEvents._debug || game.turn === localPlayer.num ) {
				Game.makeMove({
					online: true,
					x: pos1.x, 
					y: pos1.y,
					x2: pos2.x, 
					y2: pos2.y
				})
			} else {
				Controllers.GamePage.userLog( "please wait for remote player to take their turn" )
			}
		}
				
		
		parent.onmousedown = function(e) {
			// capture the event for later
			that.e = e
			
			// stop random DOM highlights occurring
			return false
		}
		
		parent.onmouseup = function(e) {
			//set the 'x1' and 'x2' parts of the move object in makeMove
			var movelimit = 15,
				diffx = that.e.layerX - e.layerX,
				magx = Math.sqrt( diffx * diffx ),
				diffy = that.e.layerY - e.layerY,
				magy = Math.sqrt( diffy * diffy )

			if ( magx > movelimit || magy > movelimit ) {
				console.log('drag event')
				doEvent( that.e, e )
			} else {
				console.log('click event')
				doEvent(e)		// run a standard 'click' event
			}
		}
	},
	
	//temp location(?) - create timer events which check the server for a move
	createTimerEvents: function(game, localPlayer) {
		
		//function to run on interval
		var intervalFunc = function() {
			if (game.turn === localPlayer.num					//don't bother to run if it is your turn (just wasted resources)
				|| game.winner !== 0)  return					//also ignore if game already ended
			
			var moveRequest = { gameId: game.gameId }			//game to check for
			var callback = function(payload) {
				var move = JSON.parse(payload)
				if (move.turnCount > game.turnCount) {			//check if opponent has actually made a move yet
					move.online = false
					Game.makeMove(move)
				}
			}
			
			API.getMove( moveRequest, callback )				//get latest move from API
			console.log("checking for opponents move...")
		}
				
		//create the main interval event
		this._myinterval = setInterval(intervalFunc, 1000)		//run once per second
	}
	
}